package com.mall.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.mall.bean.User;


public class LoginInterceptor implements HandlerInterceptor {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		String url = request.getRequestURI();

		//String personUrl = "/"+url.split("/")[2];
		//System.out.println("personUrl："+personUrl);
		/* String staticUrl = url.split("/")[0]; */
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute("user");

		if (url.indexOf("/login") >= 0) {
			System.out.println("已在注册页");
			return true;
		} else if (url.indexOf("/about-us-1") >= 0) {
			System.out.println("关于我们1");
			return true;
		} else if (url.indexOf("/autotrophy-1") >= 0) {
			System.out.println("自营1");
			return true;
		} else if (url.indexOf("/blog-1") >= 0) {
			System.out.println("博客1");
			return true;
		} else if (url.indexOf("/blog-detail-1") >= 0) {
			System.out.println("博客详情1");
			return true;
		} else if (url.indexOf("/blog-search-1") >= 0) {
			System.out.println("博客搜索页详情1");
			return true;
		} else if (url.indexOf("/contact-us-1") >= 0) {
			System.out.println("联系我们1");
			return true;
		} else if (url.indexOf("/error-404-1") >= 0) {
			System.out.println("错误404 1");
			return true;
		} else if (url.indexOf("/faq-1") >= 0) {
			System.out.println("退款详情1");
			return true;
		} else if (url.indexOf("/forgot-password") >= 0) {
			System.out.println("忘记密码1");
			return true;
		} else if (url.indexOf("/idle-goods-1") >= 0) {
			System.out.println("闲置商品详情1");
			return true;
		} else if (url.indexOf("/index-1") >= 0) {
			System.out.println("首页1");
			return true;
		} else if (url.indexOf("/index-2") >= 0) {
			System.out.println("首页2");
			return true;
		} else if (url.indexOf("/privacy-policy-1") >= 0) {
			System.out.println("隐私与政策1");
			return true;
		} else if (url.indexOf("/product-1") >= 0) {
			System.out.println("商品1");
			return true;
		} else if (url.indexOf("/product-detail-1") >= 0) {
			System.out.println("商品详情页1");
			return true;
		} else if (url.indexOf("/product-left-1") >= 0) {
			System.out.println("商品详情搜索页1");
			return true;
		} else if (url.indexOf("/register") >= 0) {
			System.out.println("注册页");
			return true;
		} else if (url.indexOf("/seller-list-1") >= 0) {
			System.out.println("卖家商品列表");
			return true;
		} else if (url.indexOf("/terms-and-conditions-1") >= 0) {
			System.out.println("条款与声明");
			return true;
		} else if (url.indexOf("/showPhoto") >= 0) {
			return true;
		} else if (url.indexOf("/") >= 0) {
			return true;
		} else if (user != null) {
			return true;
		}
		/*
		 * else if (personUrl.indexOf("/person")>=0) {
		 * request.getRequestDispatcher("/WEB-INF/static/login.jsp").forward(request,
		 * response); return false; }
		 */
		else {
			request.getRequestDispatcher("/WEB-INF/static/login.jsp").forward(request, response);
			return false;
		}


	}
}
